//	WinMain.cpp : Basic Windows Shell.

#include <windows.h>						//	Needed for Windows Applications.

//	These read the keyboard asynchronously
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

#define CLASSNAME "BasicWindow"				//	Window Class Name.
#define WINDOW_WIDTH	640					//	Window Width.
#define WINDOW_HEIGHT	480					//	Window Height.

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	//	This is the main message handler of the system
	PAINTSTRUCT	ps;			//	Used in WM_PAINT
	HDC			hdc;		//	Handle to a device context

	//	What is the message 
	switch(msg)
	{
		case WM_CREATE: 
		{
			//	Do initialization stuff here
			return(0);
		}
		break;

		case WM_PAINT:
		{
			//	Start painting
			hdc = BeginPaint(hwnd,&ps);

			//	End painting
			EndPaint(hwnd,&ps);
			return(0);
		}
		break;

		case WM_DESTROY: 
		{
			//	Kill the application			
			PostQuitMessage(0);
			return(0);
		}
		break;

		default:
		break;
	}

	//	Process any messages that we didn't take care of 
	return (DefWindowProc(hwnd, msg, wparam, lparam));
}

/***********/
/* WINMAIN */
/***********/

int WINAPI WinMain(	HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
	WNDCLASS winclass;		//	This will hold the class we create
	MSG		 msg;			//	Generic message
	HWND	 hWnd;			//	Main Window Handle.

	//	First fill in the window class stucture
	winclass.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc	= WindowProc;
	winclass.cbClsExtra		= 0;
	winclass.cbWndExtra		= 0;
	winclass.hInstance		= hinstance;
	winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName	= NULL; 
	winclass.lpszClassName	= CLASSNAME;

	//	Register the window class
	if (!RegisterClass(&winclass))
		return(0);

	//	Create the window
	if (!(hWnd = CreateWindow(	CLASSNAME,											//	Class Name.
								"Basic Window",										//	Title of the Window.
								WS_OVERLAPPEDWINDOW | WS_VISIBLE,					//	Window Flags.
								(GetSystemMetrics(SM_CXSCREEN)>>1) - (WINDOW_WIDTH>>1),	//	Window Start Point (x, y). 
								(GetSystemMetrics(SM_CYSCREEN)>>1) - (WINDOW_HEIGHT>>1),
								WINDOW_WIDTH,										//	Width of Window.
								WINDOW_HEIGHT,										//	Height of Window.
								NULL,												//	Handle to parent.
								NULL,												//	Handle to menu.
								hinstance,											//	Application Instance.
								NULL)))												//	Creation parms.
		return(0);

	//	Enter main event loop
	while(1)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{ 
			//	Test if this is a quit
			if (msg.message == WM_QUIT)
				break;
		
			//	Translate any accelerator keys
			TranslateMessage(&msg);

			//	Send the message to the window proc
			DispatchMessage(&msg);
		}
	}

	//	Return to Windows like this
	return (int)(msg.wParam);
}